/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.etrs.formathlon.entity.formation;

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import net.etrs.formathlon.entity.utilisateur.Personnel;
import net.etrs.formathlon.entity.utils.ComparableEvenThoughUnpersisted;
import net.etrs.formathlon.persistance.utils.JpaRequestNames;

/**
 * Entité symbolisant la participation d'un personnel à une formation. 
 * La participation est lié à une formation est non à une session pour 
 * une gestion facilitée.
 * @author OPIF
 */
@NamedQueries({
    @NamedQuery(name = JpaRequestNames.PARTICIPATION_A_EUE_LIEU,
    query = "select object(p) from Participation p where p.personnel = :personnel "
    + "and p.formation = :formation"),
    @NamedQuery(name = JpaRequestNames.NB_PARTICIPATIONS_POUR_FORMATION,
    query = "SELECT count(p) FROM Participation p WHERE p.formation = :formation")
})
@Entity
public class Participation extends ComparableEvenThoughUnpersisted<Participation> {
    /**
     * Identifiant.
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    /**
     * Personne ayant suivi la formation.
     */
    @ManyToOne
    private Personnel personnel;
    
    /**
     * Formation associé à la participation.
     */
    @ManyToOne
    private Formation formation;
    
    /**
     * Date à laquelle la personne à participée.
     */
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateFormation;
   
    /**
     * Booléen permettant de savoir si la donnée est archivée.
     */
    private boolean archive;

    public Participation() {
    }

    @Override
    public Long getId() {
        return id;
    }

    /**
     * Setter de l'id.
     * @param id 
     */
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Participation)) {
            return false;
        }
        Participation other = (Participation) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return String.format("Participation id=%d", id);
    }

    /**
     * @return the personnel
     */
    public Personnel getPersonnel() {
        return personnel;
    }

    /**
     * @param personnel the personnel to set
     */
    public void setPersonnel(Personnel personnel) {
        this.personnel = personnel;
    }

    /**
     * @return the formation
     */
    public Formation getFormation() {
        return formation;
    }

    /**
     * @param formation the formation to set
     */
    public void setFormation(Formation formation) {
        this.formation = formation;
    }

    /**
     * @return the dateFormation
     */
    public Date getDateFormation() {
        return dateFormation;
    }

    /**
     * @param dateFormation the dateFormation to set
     */
    public void setDateFormation(Date dateFormation) {
        this.dateFormation = dateFormation;
    }

    @Override
    public boolean isArchive() {
        return archive;
    }

    @Override
    public void setArchive(boolean archive) {
        this.archive = archive;
    }
}
